package com.guigu.youxuan.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.mapper.BackRoleMapper;
import com.guigu.youxuan.pojo.BackRole;
import com.guigu.youxuan.pojo.BackUser;
import com.guigu.youxuan.pojo.dto.GoodsDto;
import com.guigu.youxuan.service.IBackRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@Service
public class BackRoleServiceImpl extends ServiceImpl<BackRoleMapper, BackRole> implements IBackRoleService {

    @Autowired
    private BackRoleMapper backRoleMapper;
    private Long userId;

    @Override
    public List<BackRole> getBackRoleList(Long userId) {
        this.userId = userId;

        List<BackRole> roleList = backRoleMapper.getRoleListByUserId(userId);
        return roleList;
    }

    @Override
    public List<BackRole> queryBackRoleList() {
        List<BackRole> list = this.list();
        return list;
    }

    @Override
    public PageUtils<BackRole> getBackRolePage(BackRole backRole, int currentPage, int pageSize) {
        QueryWrapper<BackRole> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if(backRole != null) {
            if(!StringUtils.isEmpty(backRole.getName())) {
                wrapper.like("name",backRole.getName());
            }
        }

        PageHelper.startPage(currentPage,pageSize);
        PageHelper.orderBy("id desc");

        List<BackRole> list=this.list(wrapper);
        PageInfo<BackRole> pageInfo = new PageInfo<>(list);

        PageUtils<BackRole> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public void switchStatus(BackRole backRole) {
        if(backRole != null){
            this.updateById(backRole);
        }
    }

    @Override
    public void addRole(BackRole backRole) {
        if(backRole != null){
            this.save(backRole);
        }
    }
}
